<script setup>
	import { message } from 'ant-design-vue'
	import { SaveOutlined } from '@ant-design/icons-vue'
	import datasetApi from '@/views/plugin/dashboard/api/datasetApi'
	import { useApiDatasetStore } from '@/views/plugin/dashboard/store/apiDataset'

	const emit = defineEmits(['loadData'])
	const store = useApiDatasetStore()

	// 保存数据集配置
	const saveConfig = async () => {
		try {
			// 获取API数据集配置
			const config = store.formData
			if (!config) {
				message.error('请完善API数据集配置')
				return
			}

			const itemConfig = config.itemConfig
			if (!itemConfig.apiPath) {
				message.error('请选择API接口')
				return
			}

			if (!itemConfig.selectedColumns || itemConfig.selectedColumns.length === 0) {
				message.error('请选择数据字段')
				return
			}

			const params = {
				id: config.id,
				datasetId: config.datasetId,
				itemConfig: itemConfig,
				type: config.type || 'api_query'
			}
			await datasetApi.saveItemConfig(params)
			emit('loadData')
			message.success('保存成功')
		} catch (error) {
			console.error('保存失败:', error)
			message.error('保存失败：' + (error.message || '未知错误'))
		}
	}

	defineExpose({
		saveConfig
	})
</script>

<template>
	<a-button :disabled="!store.formData.itemConfig.apiPath" size="large" type="default" @click="saveConfig">
		<save-outlined />
		保存
	</a-button>
</template>

<style scoped></style>
